'use strict';

module.exports = app => {
    const {
        STRING,
        INTEGER,
        DATE,
        BOOLEAN
    } = app.Sequelize;
    const Admin = app.model.define('admin', {
        id: {
            primaryKey: true,
            type: INTEGER,
            autoIncrement: true
        },
        isLock: {
            type:INTEGER(1),
            defaultValue:0
        },//登录失败锁住 1,2,3 
        
        loginName: STRING(50), //用户名
        password: STRING(110),
        token: { // 
            type: STRING(1000),
            allowNull: true
        },
        authCode: INTEGER(2), //0只读 1 超级管理员
    }, {
        freezeTableName: true, // Model 对应的表名将与model名相同
        timestamps: true, //自动添加createdAt、updatedAt两个字段，分别表示创建和更新时间,且时间是实际时间,表里显示的是2019-02-13 23:50:20
        paranoid: true, //假删除
        underscored: false,
        charset: 'utf8',
        collate: 'utf8_general_ci',
    });
    Admin.sync({
            force: false
        })
        .then(() => {
            // console.log("自动创建admin表和初始化表内容 ");
            Admin.findAll({
                where: {
                    loginName: 'ksk'
                }
            }).then(res => {
                if (res.length == 0) {
                      Admin.create({
                        loginName: 'ksk',
                        password: '895b317c76b8e504c2fb32dbb4420178f60ce321',//a123456=>  895b317c76b8e504c2fb32dbb4420178f60ce321
                        authCode: 1
                    })
                      Admin.create({
                        loginName: 'admin',//普通
                        password: '605dcfac1566190a8d5a78f2a819dc8422d0cd3d',//acd13578  =>605dcfac1566190a8d5a78f2a819dc8422d0cd3d
                        authCode: 0  //只读 
                    })
                }
            })
        })
        .catch(e => {
            console.log('创建admin表失败!')
        })
    return Admin;
};